Imports BusinessLogicLayer.BLMain
Imports DataAccessLayer.DLMain
Imports UniversalLayer.ULMain
Imports UniversalLayer.ULDataHandler

''' <summary>
''' NO COMMENTS
''' </summary>
''' <remarks></remarks>
Public Class DLSettings

    ''' <summary>
    ''' NO COMMENTS
    ''' </summary>
    ''' <remarks></remarks>
    Public Enum eSettingsReturnType
        DLSettings = 0
        DataTable = 1
        Array = 2
        Value = 3
    End Enum

    Private iPKey As Integer
    Private sMeta As String
    Private sID As String
    Private sValue As String
    Private bDefaultValue As Boolean
    Private iSort As Integer

    Private dtSettingsMain As DataTable
    'Dim _BLMain As BLMain
    'Dim _DLMain As DLMain
    Public Const defSettingsReturnType As eSettingsReturnType = eSettingsReturnType.DLSettings

    ''' <summary>
    ''' Gets SettingsMain.PKey
    ''' </summary>
    ''' <value>SettingsMain.PKey</value>
    ''' <returns>SettingsMain.PKey</returns>
    ''' <remarks></remarks>
    Public Property cPKey() As Integer
        Get
            Return iPKey
        End Get
        Set(ByVal value As Integer)
            iPKey = value
        End Set
    End Property

    ''' <summary>
    ''' Gets/Sets SettingsMain.Meta
    ''' </summary>
    ''' <value>SettingsMain.Meta</value>
    ''' <returns>SettingsMain.Meta</returns>
    ''' <remarks></remarks>
    Public Property cMeta() As String
        Get
            Return sMeta
        End Get
        Set(ByVal value As String)
            sMeta = value
        End Set
    End Property

    ''' <summary>
    ''' Gets/Sets SettingsMain.ID
    ''' </summary>
    ''' <value>SettingsMain.ID</value>
    ''' <returns>SettingsMain.ID</returns>
    ''' <remarks></remarks>
    Public Property cID() As String
        Get
            Return sID
        End Get
        Set(ByVal value As String)
            sID = value
        End Set
    End Property

    ''' <summary>
    ''' Gets/Sets SettingsMain.Value
    ''' </summary>
    ''' <value>SettingsMain.Value</value>
    ''' <returns>SettingsMain.Value</returns>
    ''' <remarks></remarks>
    Public Property cValue() As String
        Get
            Return sValue
        End Get
        Set(ByVal value As String)
            sValue = value
        End Set
    End Property

    ''' <summary>
    ''' Gets/Sets SettingsMain.DefaultValue
    ''' </summary>
    ''' <value>SettingsMain.DefaultValue</value>
    ''' <returns>SettingsMain.DefaultValue</returns>
    ''' <remarks></remarks>
    Public Property cDefaultValue() As Boolean
        Get
            Return bDefaultValue
        End Get
        Set(ByVal value As Boolean)
            bDefaultValue = value
        End Set
    End Property

    ''' <summary>
    ''' Gets/Sets SettingsMain.Sort
    ''' </summary>
    ''' <value>SettingsMain.Sort</value>
    ''' <returns>SettingsMain.Sort</returns>
    ''' <remarks></remarks>
    Public Property cSort() As Integer
        Get
            Return iSort
        End Get
        Set(ByVal value As Integer)
            iSort = value
        End Set
    End Property

    ''' <summary>
    ''' NO COMMENTS
    ''' </summary>
    ''' <param name="dtSettings"></param>
    ''' <remarks></remarks>
    Public Sub New(Optional ByRef dtSettings As DataTable = Nothing)
        Try
            If IsEmpty(dtSettingsMain) Then
                dtSettingsMain = New DataTable
                'dtSettings.Columns.Add("Meta")
                'dtSettings.Columns.Add("ID")
                'dtSettings.Columns.Add("Value")
                'dtSettings.Columns.Add("DefaultValue")
                'dtSettings.Columns.Add("Sort")
                ''dtSettingsMain = CType(FetchSettings("", "", "", DLSettings.eSettingsReturnType.DataTable), DataTable)
                dtSettingsMain = FillSettings()
            Else
                'dtSettings.Columns.Add("Meta")
                'dtSettings.Columns.Add("ID")
                'dtSettings.Columns.Add("Value")
                'dtSettings.Columns.Add("DefaultValue")
                'dtSettings.Columns.Add("Sort")
                dtSettingsMain = dtSettings.DefaultView.ToTable("SettingsMain")
            End If

        Finally
            'NO FINALLY
        End Try
    End Sub

    '''' <summary>
    '''' NO COMMENTS
    '''' </summary>
    '''' <param name="sSettingsMainPKey"></param>
    '''' <remarks></remarks>
    'Public Sub New(ByVal sSettingsMainPKey As Integer)
    '    iPKey = sSettingsMainPKey
    'End Sub

#Region "Fetch Settings"

    ''' <summary>
    ''' NO COMMENTS
    ''' </summary>
    ''' <param name="sCondition"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function FillSettings(Optional ByVal sCondition As String = "") As DataTable
        Dim sSQL As String
        Dim dtSettings As DataTable
        Dim _DLReader As DLReader

        Try
            dtSettings = New DataTable
            _DLReader = New DLReader("DLSettings")
            If IsEmpty(sCondition) Then sCondition = "(1=1)"
            sSQL = "SELECT PKey, Meta, ID, Value, DefaultValue, Sort FROM SettingsMain WHERE Status<>'d' AND " & sCondition & " ORDER BY Meta ASC, ID ASC, Sort ASC"
            _DLReader.ExecuteSQL(sSQL, dtSettings, "SettingsMain")
            Return dtSettings
        Finally
            sSQL = Nothing
            _DLReader = Nothing
            dtSettings = Nothing
        End Try
    End Function

    ''' <summary>
    ''' NOT COMPLETED: Initializes DLSettings properties corresponding to single setting DataRow
    ''' </summary>
    ''' <param name="drSettings">DataRow containing single setting</param>
    ''' <remarks></remarks>
    Private Sub InitializeOneSetting(ByVal drSettings As DataRow)
        Try
            cPKey = CInt(drSettings.Item("PKey").ToString)
            cMeta = drSettings.Item("Meta").ToString
            cID = drSettings.Item("ID").ToString
            cValue = drSettings.Item("Value").ToString
            cDefaultValue = CBool(drSettings.Item("Value").ToString)
            cSort = CInt(drSettings.Item("Sort").ToString)
        Finally
            drSettings = Nothing
        End Try
    End Sub

    ''' <summary>
    ''' Fetches single setting as Value
    ''' </summary>
    ''' <param name="sMeta">Settings Meta</param>
    ''' <param name="sID">Settings ID</param>
    ''' <param name="sCondition">Additional Condition</param>
    ''' <returns>Value of single setting</returns>
    ''' <remarks></remarks>
    Friend Function FetchOneSetting(ByVal sMeta As String, ByVal sID As String, Optional ByVal sCondition As String = "") As Object
        'Dim sSQL As String = "SELECT PKey, Meta, ID, Value, DefaultValue, Sort FROM SettingsMain WHERE Status<>'d' AND Meta='" & sMeta & "' AND ID='" & sID & "' AND " & sCondiiton & " ORDER BY Sort ASC"
        'Dim dtSettings As New DataTable
        '_DLMain.ExecuteSQL(sSQL, dtSettingsz)

        Try
            Return FetchSettings(sMeta, sID, sCondition, eSettingsReturnType.Value)
        Finally
            sMeta = Nothing
            sID = Nothing
            sCondition = Nothing
            'sSQL = Nothing
            'dtSettings = Nothing
        End Try
    End Function

    ''' <summary>
    ''' Fetches single setting as Value from specified DataTable
    ''' </summary>
    ''' <param name="dtSettings">Settings DataTable</param>
    ''' <param name="sMeta">Settings Meta</param>
    ''' <param name="sID">Settings ID</param>
    ''' <param name="sCondition">Additional Condition</param>
    ''' <returns>Value of specified setting</returns>
    ''' <remarks></remarks>
    Public Shared Function FetchOneSetting(ByVal dtSettings As DataTable, ByVal sMeta As String, ByVal sID As String, Optional ByVal sCondition As String = "") As Object
        Try
            If IsEmpty(sCondition) Then sCondition = "(1=1)"
            If Not IsEmpty(sID) Then sCondition = "ID = '" & sID & "' AND " & sCondition
            If Not IsEmpty(sMeta) Then sCondition = "Meta = '" & sMeta & "' AND " & sCondition

            ''Option 1: _______________________________________
            Dim dr() As DataRow = dtSettings.Select(sCondition, "Meta ASC, Sort ASC")
            If dr.Length = 1 Then
                If Not IsEmpty(dr(0).Item("Value")) Then Return dr(0).Item("Value") _
                Else Return dr(0).Item("DefaultValue")
            Else : Return Nothing
            End If
            ''Option 2: _______________________________________
            'DLMain.CopyDataTable(dtSettings, dtSettings, sCondition, "", "Settings_Value", True, "Meta ASC, Sort ASC")
            'If dtSettings.Rows.Count = 1 Then Return dtSettings.Rows(0).Item("Value") Else Return Nothing
            ''Option 3: _______________________________________
            'Return FetchSettings(sMeta, sID, sCondiiton, eSettingsReturnType.Value)
        Finally
            dtSettings = Nothing
            sMeta = Nothing
            sID = Nothing
            sCondition = Nothing
            'sSQL = Nothing
            'dtSettings = Nothing
        End Try
    End Function

    ''' <summary>
    ''' Fetches all settings as DataTable
    ''' </summary>
    ''' <returns>DataTable containing all settings</returns>
    ''' <remarks></remarks>
    Public Function FetchAllSettings() As DataTable
        Return CType(FetchSettings("", "", "", DLSettings.eSettingsReturnType.DataTable), DataTable)
    End Function

    ''' <summary>
    ''' NO COMMENTS (try to retrieve from dtSettings as it is already being initialized)
    ''' </summary>
    ''' <param name="sMeta"></param>
    ''' <param name="sID"></param>
    ''' <param name="sCondiiton">Additional Condition</param>
    ''' <param name="iSettingsReutrnType"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function FetchSettings(ByVal sMeta As String, Optional ByVal sID As String = "", Optional ByVal sCondiiton As String = "", Optional ByVal iSettingsReutrnType As DLSettings.eSettingsReturnType = DLSettings.defSettingsReturnType) As Object
        Dim dtSettings As DataTable
        Dim sSort As String
        Dim sCols As String
        Dim sTableName As String

        Try
            dtSettings = New DataTable
            If IsEmpty(sCondiiton) Then sCondiiton = "(1=1)"
            If IsEmpty(sMeta) Then
                'sSQL = "SELECT PKey, Meta, ID, Value, DefaultValue, Sort FROM SettingsMain WHERE Status<>'d' AND " & sCondiiton & " ORDER BY Meta ASC, ID ASC, Sort ASC"
                sSort = "Meta ASC, ID ASC, Sort ASC"
            ElseIf IsEmpty(sID) Then
                'sSQL = "SELECT PKey, Meta, ID, Value, DefaultValue, Sort FROM SettingsMain WHERE Status<>'d' AND Meta='" & sMeta & "' AND " & sCondiiton & " ORDER BY Sort ASC"
                sCondiiton = "Meta='" & sMeta & "' AND " & sCondiiton
                sSort = "Sort ASC"
            Else
                'sSQL = "SELECT PKey, Meta, ID, Value, DefaultValue, Sort FROM SettingsMain WHERE Status<>'d' AND Meta='" & sMeta & "' AND ID='" & sID & "' AND " & sCondiiton & " ORDER BY Sort ASC"
                sCondiiton = "Meta='" & sMeta & "' AND ID='" & sID & "' AND " & sCondiiton
                sSort = "Sort ASC"
            End If
            If IsEmpty(sSort) Then sSort = "Meta ASC, ID ASC, Sort ASC"
            sTableName = "SettingsMain" & IIf(IsEmpty(sMeta), "", "_" & sMeta & IIf(IsEmpty(sID), "", "_" & sID).ToString).ToString

            'Fetch Settings here
            '' ''Dim dvSettingsFilter As New DataView(dtSettingsMain, sCondiiton, sSort, DataViewRowState.OriginalRows)
            '' ''Dim arrCols() As String = "Meta,ID,Value,DefaultValue,Sort".Split(","c)
            '' ''Try
            '' ''    dtSettings = dvSettingsFilter.ToTable(sTableName, True, arrCols)
            '' ''    Erase arrCols
            '' ''Finally
            '' ''    sTableName = Nothing
            '' ''    dvSettingsFilter = Nothing
            '' ''End Try
            sCols = "Meta,ID,Value,DefaultValue,Sort"
            CopyDataTable(dtSettingsMain, dtSettings, sCondiiton, sCols, sTableName, True, sSort, False)
        Finally
            sSort = Nothing
            sCols = Nothing
            sTableName = Nothing
        End Try

        'Returns
        If Not IsEmpty(sMeta) AndAlso Not IsEmpty(sID) AndAlso dtSettings.Rows.Count <> 1 Then _
            Return Nothing 'There should be a unique combination for Meta/ID

        Dim arrDLSettings() As DLSettings
        Dim drSettings As DataRow '= _DLSettings.dtSettingsMain.Rows(0)
        Try
            Select Case iSettingsReutrnType
                Case DLSettings.eSettingsReturnType.DLSettings
                    arrDLSettings(dtSettings.Rows.Count - 1) = New DLSettings

                    For i As Integer = 0 To dtSettings.Rows.Count - 1
                        drSettings = dtSettings.Rows(i)
                        arrDLSettings(i) = New DLSettings
                        With arrDLSettings(i)
                            arrDLSettings(i).InitializeOneSetting(drSettings)
                            '.cPKey = CInt(drSettingsMain.Item("PKey").ToString)
                            '.cMeta = drSettingsMain.Item("Meta").ToString
                            '.cID = drSettingsMain.Item("ID").ToString
                            '.cValue = drSettingsMain.Item("Value").ToString
                            '.cDefaultValue = CBool(drSettingsMain.Item("Value").ToString)
                            '.cSort = CInt(drSettingsMain.Item("Sort").ToString)
                        End With
                        drSettings = Nothing
                    Next
                    Return arrDLSettings

                Case DLSettings.eSettingsReturnType.DataTable
                    Return dtSettings

                Case DLSettings.eSettingsReturnType.Array
                    Return Nothing

                Case eSettingsReturnType.Value
                    If Not IsEmpty(sMeta) And Not IsEmpty(sID) And dtSettings.Rows.Count = 1 Then
                        If Not IsEmpty(dtSettings.Rows(0).Item("Value")) Then Return dtSettings.Rows(0).Item("Value") _
                        Else Return dtSettings.Rows(0).Item("DefaultValue")
                    Else : Return Nothing
                    End If

                Case Else
                    Return Nothing

            End Select

        Finally
            sSort = Nothing
            dtSettings = Nothing
            Erase arrDLSettings
        End Try
    End Function

#End Region
End Class
